package org.example.product.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.example.core.utils.PageUtils;
import org.example.product.dao.*;
import org.example.product.entity.SkuInfoEntity;
import org.example.product.service.SkuInfoService;
import org.example.product.to.cart.FullReductionTo;
import org.example.product.to.cart.LadderTo;
import org.example.product.to.cart.SkuInfoTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author 小杰_cc
 * @version 1.0
 */
@Service("skuInfoService")
public class SkuInfoServiceImpl implements SkuInfoService {

    @Autowired
    private SkuInfoDao skuInfoDao;
    @Autowired
    private SkuAttributeDao skuAttributeDao;


    //查询sku信息集合
    @Override
    public PageUtils querySkuInfoList(String skuTitle, Long categoryId, Long brandId, BigDecimal priceMax, BigDecimal priceMin, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<SkuInfoEntity> entities = skuInfoDao.getSkuInfoList(skuTitle,categoryId,brandId,priceMax,priceMin);
        return new PageUtils(new PageInfo<>(entities));
    }

    // 查看购物车时所需要的数据
    @Override
    public Map<Long, SkuInfoTo> queryCartSkus(List<Long> skuIds) {
        // 基本信息
        List<SkuInfoTo> skuInfoTos = skuInfoDao.getSkuInfoCartBySkuIds(skuIds);
        // 查询销售属性
        for (SkuInfoTo skuInfoTo : skuInfoTos) {
            List<String> attrValues = skuAttributeDao.getCartAttrValue(skuInfoTo.getSkuId());
            skuInfoTo.setAttribute(String.join(";", attrValues));
        }
        Map<Long, SkuInfoTo> skuInfoToMap = skuInfoTos.stream().collect(Collectors.toMap(SkuInfoTo::getSkuId, Function.identity(), (oldValue, newValue) -> newValue));
        return skuInfoToMap;
    }
}